<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="输入搜索：">
            <el-input
              style="width: 203px"
              clearable
              v-model="listQuery.productName"
              placeholder="产品名称"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button class="btn-add" @click="handleAddProduct()" size="mini">
        添加
      </el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="productTable"
        :data="list"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column label="产品名称" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.productName }}</template>
        </el-table-column>
        <el-table-column label="产品图片" align="center">
          <template slot-scope="scope"
            ><img
              v-for="(item, index) in scope.row.pictures"
              :key="index"
              style="height: 80px"
              :src="item"
          /></template>
        </el-table-column>
        <el-table-column label="购买份数限制" align="center">
          <template slot-scope="scope">{{
            scope.row.purchaseLimitNum
          }}</template>
        </el-table-column>
        <el-table-column label="犹豫期(天)" align="center">
          <template slot-scope="scope"
            >{{ scope.row.freeLookPeriodDay }}
          </template>
        </el-table-column>

        <el-table-column label="创建时间" align="center">
          <template slot-scope="scope">{{
            scope.row.createdAt | formatDateTime
          }}</template>
        </el-table-column>
        <el-table-column label="更新时间" align="center">
          <template slot-scope="scope">{{
            scope.row.updatedAt | formatDateTime
          }}</template>
        </el-table-column>
        <el-table-column label="上架状态" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleStatusChange(scope.$index, scope.row)"
              :active-value="publishStatusEnum.YES"
              :inactive-value="publishStatusEnum.NO"
              v-model="scope.row.publishStatus"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tag type="success" v-if="!scope.row.isDeleted">未删除</el-tag>
            <el-tag type="danger" v-else>已删除</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark">
        </el-table-column>

        <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope">
            <el-row>
              <router-link
                :to="{
                  name: 'updateProduct',
                  query: {
                    id: scope.row.id,
                  },
                }"
              >
                <el-button size="mini" type="text">
                  编辑
                </el-button></router-link
              >
            </el-row>
            <el-row>
              <router-link
                :to="{
                  name: 'productBindFile',
                  query: {
                    id: scope.row.id,
                  },
                }"
              >
                <el-button size="mini" type="text">
                  编辑文件
                </el-button></router-link
              >
            </el-row>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="PAGE_SIZES"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { changeStatus, fetchProductList } from "@/api/product";
import { formatDate } from "@/utils/date";
import { publishStatusEnum } from "@/utils/constant/pms";
import { DEFAULT_PAGE_SIZE, PAGE_SIZES } from "@/utils/constant/system";

const defaultListQuery = {
  productName: null,
  pageNum: 1,
  pageSize: DEFAULT_PAGE_SIZE,
};
export default {
  name: "productList",
  computed: {
    publishStatusEnum() {
      return publishStatusEnum;
    },
  },
  data() {
    return {
      PAGE_SIZES,
      editSkuInfo: {
        dialogVisible: false,
        productId: null,
        productSn: "",
        productAttributeCategoryId: null,
        stockList: [],
        productAttr: [],
        keyword: null,
      },
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: true,
      selectProductCateValue: null,
      publishStatusOptions: [
        {
          value: 1,
          label: "上架",
        },
        {
          value: 0,
          label: "下架",
        },
      ],
      verifyStatusOptions: [
        {
          value: 1,
          label: "审核通过",
        },
        {
          value: 0,
          label: "未审核",
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  filters: {
    verifyStatusFilter(value) {
      if (value === 1) {
        return "审核通过";
      } else {
        return "未审核";
      }
    },
    formatDateTime(time) {
      if (time == null || time === "") {
        return "N/A";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    },
  },
  methods: {
    handleStatusChange(index, row) {
      this.$confirm("是否要修改上架状态?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          changeStatus(row.id, row.publishStatus).then((response) => {
            this.$message({
              type: "success",
              message: "修改成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消修改",
          });
          this.getList();
        });
    },
    getProductSkuSp(row, index) {
      let spData = JSON.parse(row.spData);
      if (spData != null && index < spData.length) {
        return spData[index].value;
      } else {
        return null;
      }
    },
    getList() {
      this.listLoading = true;
      fetchProductList(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleAddProduct() {
      this.$router.push({ path: "/pms/addProduct" });
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
  },
};
</script>
<style></style>
